package com.food.user.interceptor;

import com.food.user.util.JwtUtil;
import com.food.user.util.RedisUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisUtil redisUtil;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //预检一定放前面
        String requestMethodName = request.getMethod();
        if (requestMethodName.equals("OPTIONS")){return true;}
        System.out.println("前置处理");
        System.out.println(request.getRequestURI()+"请求进入过滤器");
        //判断jwt的合法性
        //先要取出jwt
        String accessToken = request.getHeader("accesstoken");
        System.out.println(accessToken);
        if(accessToken==null){
            throw new Exception("还未登录");
        }else {
            //解析
            //会抛出异常
            try{
                JwtUtil.parseToken(accessToken);
            } catch (ExpiredJwtException e){
                //通过accessToken从redis中取出refreshToken
                String refreshToken = redisUtil.getValue(accessToken);
                if(refreshToken==null){
                    throw e;
                }else {
                    //解析长Token
                    Claims claims = JwtUtil.parseToken(refreshToken);
                    //长token未过期
                    Object userId = claims.get("sub");
                    HashMap<String, Object> body = new HashMap<>();
                    body.put("sub",userId);
                    Integer min=30;
                    String newAccessToken = JwtUtil.createToken(body, min);
                    //签发长token
                    String newRefreshToken = JwtUtil.createToken(body, 8*60);
                    //保存新的accessToekn
                    redisUtil.setValue(newAccessToken,newRefreshToken,8*60*60L);
                    //添加响应头
                    response.setHeader("accessToken",newAccessToken);
                }
            }
        }
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("后置处理");
        //删除redis中的旧的accessToken
        String accessToken = request.getHeader("accessToken");
        if(accessToken!=null){
            try {
                JwtUtil.parseToken(accessToken);
            }catch (ExpiredJwtException e){
                redisUtil.delete(accessToken);
            }
        }
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("响应前处理");
    }
}
